package cn.hpclub.server.controller.admin;

import cn.hpclub.server.model.LoginCount;
import cn.hpclub.server.service.AccountService;
import cn.hpclub.server.service.LoginCountService;
import cn.hpclub.server.util.ExcelRenderUtil;
import cn.hpclub.server.util.InterfaceUtil;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by wanglin on 2017/2/28.
 */
public class LoginCountController extends BaseAdminController<LoginCount> {

    private static Logger logger = LoggerFactory.getLogger(LoginCountController.class);
    private static final String QUERY_DATE_FORMAT = "yyyy-MM-dd";
    private static final String END_DATE          = "end_date";
    private static final String START_DATE        = "start_date";
    private static final String ORG_ID            = "org_id";
    /**
     * 登录统计
     */
    public void list(){

        // 给筛选项所属机构赋值
        int defaultOrgId = initOrgTree();

        ConcurrentHashMap<String, String> searchInfo = getSearchFilter();
        if(searchInfo.get(ORG_ID) == null){
            // 如果是第一次，给选中的机构赋值为默认的顶级机构。
            searchInfo.put(ORG_ID, String.valueOf(defaultOrgId));
        }
        findByPageForSearch(LoginCountService.service.getSqlHead(), LoginCountService.service.getSqlExceptSelect(searchInfo));
        setAttrs(searchInfo);
        render("/admin/login_count.tpl");
    }

    /**
     * 登录统计报表导出
     */
    public void export(){

        // 给筛选项所属机构赋值
        int defaultOrgId = initOrgTree();

        ConcurrentHashMap<String, String> searchInfo = getSearchFilter();
        if(searchInfo.get(ORG_ID) == null){
            // 如果是第一次，给选中的机构赋值为默认的顶级机构。
            searchInfo.put(ORG_ID, String.valueOf(defaultOrgId));
        }
        SXSSFWorkbook workbook = new LoginCountService().service.getExcel(searchInfo);
        render(new ExcelRenderUtil(workbook, "登录用户统计"));
    }

    /**
     * 查看登录统计报表详情列表
     */
    public void detail(){
        // 给筛选项所属机构赋值
        int defaultOrgId = initOrgTree();

        ConcurrentHashMap<String, String> searchInfo = getSearchFilter();
        if(searchInfo.get(ORG_ID) == null){
            // 如果是第一次，给选中的机构赋值为默认的顶级机构。
            searchInfo.put(ORG_ID, String.valueOf(defaultOrgId));
        }
        findByPageForSearch(LoginCountService.service.getDetailSqlHead(), LoginCountService.service.getDetailSqlExceptSelect(searchInfo));
        if(searchInfo.get(ORG_ID).equals("0")){
            setAttr(ORG_ID, searchInfo.get("city_code"));
        }
        render("/admin/login_count_detail.tpl");
    }

    /**
     * 登录统计报表详情导出
     */
    public void detailExport(){
        // 给筛选项所属机构赋值
        int defaultOrgId = initOrgTree();

        ConcurrentHashMap<String, String> searchInfo = getSearchFilter();
        if(searchInfo.get(ORG_ID) == null){
            // 如果是第一次，给选中的机构赋值为默认的顶级机构。
            searchInfo.put(ORG_ID, String.valueOf(defaultOrgId));
        }
        if(searchInfo.get(ORG_ID).equals(searchInfo.get("city_code"))){
            searchInfo.put(ORG_ID, "0");
        }
        SXSSFWorkbook workbook = new LoginCountService().service.getDetailExcel(searchInfo);
        render(new ExcelRenderUtil(workbook, "登录详情报表"));
    }

    /**
     * @Title: getSearchFilter
     * @Description:获取查询条件
     * @return ConcurrentHashMap<String,String>
     */
    private ConcurrentHashMap<String, String> getSearchFilter(){
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        int adminId = InterfaceUtil.getAdminId();
        int userRoleType = AccountService.service.getUserRoleType(adminId);
        logger.debug("userRoleType=" + userRoleType);
        // if(userRoleType == Constant.ROLE_TYPE_SUPPLIER_ADMIN){
        // // 角色类型为供应商管理角色时，只能查看由该帐号创建的商品的订单
        // searchInfo.put("creator", String.valueOf(adminId));
        // } else{
        // 其他角色根据机构关系限制数据
        //searchInfo.put(ORG_ID, String.valueOf(InterfaceUtil.getOrgId()));
        // }
        setQueryTime(searchInfo);
        return searchInfo;
    }

    /**
     * @Title: setQueryTime
     * @Description:设置查询时间
     * @param searchInfo:查询参数列表
     */
    private void setQueryTime(ConcurrentHashMap<String, String> searchInfo){
        if(searchInfo.get(START_DATE) == null){
            // Calendar calendar = Calendar.getInstance();
            // calendar.setTime(new Date());
            // calendar.set(Calendar.DATE, 1);
            // SimpleDateFormat sdf = new SimpleDateFormat(QUERY_DATE_FORMAT);
            // String startDate = sdf.format(calendar.getTime());
            // searchInfo.put(START_DATE, startDate);
            SimpleDateFormat sdf = new SimpleDateFormat(QUERY_DATE_FORMAT);
            String startDate = sdf.format(new Date());
            searchInfo.put(START_DATE, startDate);
        }
        if(searchInfo.get(END_DATE) == null){
            SimpleDateFormat sdf = new SimpleDateFormat(QUERY_DATE_FORMAT);
            String startDate = sdf.format(new Date());
            searchInfo.put(END_DATE, startDate);
        }
    }

    /**
     * 保存搜索条件
     *
     * @param searchInfo
     */
    public void setAttrs(ConcurrentHashMap<String, String> searchInfo){
        for(Map.Entry<String, String> item : searchInfo.entrySet()){
            // System.out.println(item.getKey() + ":" + item.getValue());
            setAttr(item.getKey(), item.getValue());
        }
    }

}
